package com.lightjet.macross.base.upms.repository;

import com.lightjet.macross.base.upms.domain.SysUser;
import com.lightjet.macross.common.core.BaseRepository;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("sysUserRepository")
public interface SysUserRepository extends BaseRepository<SysUser, String> {

    SysUser findDistinctById(String id);

    SysUser findDistinctByUsername(String username);

    @Query("select t from SysUser t where t.displayname like :displayname and t.username like :username and t.workno like :workno and  (t.affiliation.id = :deptid or :deptid='-1' )")
    Page<SysUser> findPagedByFilters(@Param("username") String username, @Param("displayname") String displayname, @Param("workno") String workno, @Param("deptid") String deptid, Pageable pageable);

    @Query("select t from SysUser t where COALESCE(t.displayname,'') like :displayname and COALESCE(t.username,'') like :username and COALESCE(t.mobile,'') like :mobile and COALESCE(t.email,'') like :email")
    List<SysUser> findAllByFilter(@Param("username")String username,@Param("displayname")String displayname,@Param("email")String email,@Param("mobile")String mobile);

    @Query("select t from SysUser t where t.displayname like :keywords or t.username like :keywords or COALESCE(t.email,'!@#') like :keywords or COALESCE(t.workno,'!@#') like :keywords or COALESCE(t.affiliation.displayname,'!@#') like :keywords ")
    Page<SysUser> findPageByKeyWords(@Param("keywords") String keywords,Pageable pageable);
}
